package gamagora.game.states
{
	import gamagora.game.entity.OSBird;
	import gamagora.game.entity.OSMonkey;
	import gamagora.game.resources.OSImages;
	import gamagora.game.resources.OSSounds;
	import gamagora.game.utils.CreditsList;
	import gamagora.game.utils.OSBirdTrio;
	
	import org.flixel.FlxG;
	import org.flixel.FlxObject;
	import org.flixel.FlxSound;
	import org.flixel.FlxSprite;
	import org.flixel.FlxState;
	
	public class OSEndJungleState extends FlxState
	{
		private var SPEED_SCROLL_1:Number = 50.0;
		private var SPEED_SCROLL_2:Number = 100.0;
		private var SPEED_SCROLL_3:Number = 200.0;
		
		private var _blackFade:FlxSprite;
		private var _completeFade:Boolean;
		private var _background:FlxSprite;
		private var _back1:FlxSprite;
		private var _back2:FlxSprite;
		private var _back3:FlxSprite;
		private var _monkey1:OSMonkey;
		private var _monkey2:OSMonkey;
		private var _monkey3:OSMonkey;
		private var _monkey4:OSMonkey;
		private var _birds1:OSBirdTrio;
		private var _birds2:OSBirdTrio;
		private var _birds3:OSBirdTrio;
		private var _creditList:CreditsList;
		private var _goToMenu:Boolean;
		private var _cutScene:Boolean;
		
		private var _music:FlxSound;
		
		
		public override function create():void
		{
			//Backgrounds
			_background = new FlxSprite(0, 0, OSImages.TITLE_BACKGROUND);
			_back1 = new FlxSprite(0, 0, OSImages.JUNGLE_END_1);
			_back2 = new FlxSprite(0, 0, OSImages.JUNGLE_END_2);
			_back3 = new FlxSprite(0, 0, OSImages.JUNGLE_END_3);
			add(_background);
			add(_back1);
			add(_back2);
			add(_back3);
			
			//Monkeys
			_monkey1 = addMonkey(350, 36);
			_monkey2 = addMonkey(400, 38);
			_monkey3 = addMonkey(460, 35);			
			_monkey4 = addMonkey(500, 40, true);
			
			//Birds
			_birds1 = new OSBirdTrio(190, 250);
			_birds2 = new OSBirdTrio(260, 320);
			_birds3 = new OSBirdTrio(580, 290);
			add(_birds1);
			add(_birds2);
			add(_birds3);
			
			//Credits
			_creditList = new CreditsList();
			
			//Black fade
			_blackFade = new FlxSprite();
			_blackFade.makeGraphic(FlxG.width, FlxG.height, 0xff000000);
			add(_blackFade);
			
			
			_music = new FlxSound();
			_music.loadEmbedded(OSSounds.MUSIC, true);
			_music.volume = 0.5;
			_music.fadeIn(2);
		}
		
		public override function update():void
		{
			super.update();
			
			if(!_cutScene)
			{
				if(FlxG.keys.any())
				{
					_music.fadeOut(2, true);
					FlxG.fade(0xff000000, 2, goBackToMenu);
					_cutScene = true;	
				}
			
				if(!_completeFade)
				{
					_blackFade.alpha -= FlxG.elapsed;
					if(_blackFade.alpha <= 0)
					{
						_completeFade = true;
						add(_creditList);
					}
				}
				else
				{
					if(_creditList.complete && !_goToMenu)
					{
						_monkey1.x += FlxG.elapsed * 180;
						_monkey2.x += FlxG.elapsed * 180;
						_monkey3.x += FlxG.elapsed * 180;
						_monkey4.x += FlxG.elapsed * 180;
						_birds1.move(FlxG.elapsed * 180, FlxG.elapsed * 200)
						_birds2.move(FlxG.elapsed * 180, FlxG.elapsed * 200);
						_birds3.move(FlxG.elapsed * 180, FlxG.elapsed * 200);
						
						if(_monkey1.x >= FlxG.width + 60)
						{
							_music.fadeOut(2, true);
							FlxG.fade(0xff000000, 3, goBackToMenu);
							_goToMenu = true;
						}
					}
				}
				
				if(!_creditList.complete)
				{
					_back1.x -= FlxG.elapsed * SPEED_SCROLL_1;
					if(_back1.x <= -FlxG.width)
					{
						_back1.x = 0;
					}
					
					_back2.x -= FlxG.elapsed * SPEED_SCROLL_2;
					if(_back2.x <= -FlxG.width)
					{
						_back2.x = 0;
					}
					
					_back3.x -= FlxG.elapsed * SPEED_SCROLL_3;
					if(_back3.x <= -FlxG.width)
					{
						_back3.x = 0;
					}
				}
			}
			_music.update();
		}
		
		public override function draw():void
		{
			_background.draw();
			
			drawScrollBackground(_back1);
			drawScrollBackground(_back2);
			
			_birds1.draw();
			_birds2.draw();
			_birds3.draw();
			
			drawScrollBackground(_back3);
			
			//Monkeys and birds
			_monkey1.draw();
			_monkey2.draw();
			_monkey3.draw();
			_monkey4.draw();
			
			//Credits
			_creditList.draw();
			
			//Fade
			if(!_completeFade)
			{
				_blackFade.draw();
			}
		}
		
		private function drawScrollBackground(background:FlxSprite):void
		{
			background.draw();
			background.x += FlxG.width;
			background.draw();
			background.x -= FlxG.width;
		}
		
		private function addMonkey(x:Number, frameRate:uint = 40, ribbon:Boolean = false):OSMonkey
		{
			var monkey:OSMonkey = new OSMonkey(x, 462, false, 0, frameRate, ribbon);
			monkey.animOnly = true;
			monkey.canCry = false;
			monkey.facing = FlxObject.LEFT;
			monkey.play(OSMonkey.RUN);
			add(monkey);
			return monkey;
		}
		
		private function goBackToMenu():void
		{
			FlxG.switchState(new OSMenuState());
		}
	}
}
